/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helwen.restaurantbooking.datalayer.dao.daoimpl;

import com.helwen.restaurantbooking.datalayer.dao.AbstractDao;
import com.helwen.restaurantbooking.datalayer.dao.IBookingDao;
import com.helwen.restaurantbooking.datalayer.entities.TblBooking;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Nghiata
 */
@Repository("bookingDao")
public class BookingDaoImpl extends AbstractDao implements IBookingDao {

    public List<TblBooking> getAllBookingInfo() {
        return getHibernateTemplate().loadAll(TblBooking.class);
    }

    public List<TblBooking> getAllBookingInfoOrderby(String columnName, String sort) {
        Query queryString=getSession().createQuery("from TblBooking b order by b." + columnName + " " + sort);
        return queryString.list();
    }

    public List<TblBooking> getBookingByCriteria(String searchName, String searchContact) {
        return getHibernateTemplate().find("from TblBooking b where b.customerName like ? and b.contact like ? ", "%" + searchName + "%", "%" + searchContact + "%");
    }

    public List<TblBooking> getBookingByCriteriaOrderBy(String searchName, String searchContact, String columnName, String sort) {
        Query queryString=getSession().createQuery("from TblBooking b where b.customerName like ? and b.contact like ? order by  b." + columnName + " " + sort);
        queryString.setParameter(0,"%" + searchName + "%");
        queryString.setParameter(1,"%" + searchContact + "%");
        return queryString.list();
    }

    public void saveOrUpdate(TblBooking tblBooking) {
        getHibernateTemplate().saveOrUpdate(tblBooking);
    }

    public TblBooking getBookingById(Integer id) {
        return getHibernateTemplate().get(TblBooking.class, id);
    }

    public List<TblBooking> getBookingByNumberDateTime(Integer number, Date date, Short time) {
        return getHibernateTemplate().find("from TblBooking t where t.tblTable.tableNumber=? and t.reservationDate=? and t.reservationTime=?", number, date, time);
    }

    public List<TblBooking> getWaitingBooking() {
        return getHibernateTemplate().find("from TblBooking t where t.status=0");
    }

    public void deleteAll() {
        getHibernateTemplate().deleteAll(getAllBookingInfo());
    }

    public void bulkInsert(Collection<TblBooking> tblBookings) {
        getHibernateTemplate().saveOrUpdateAll(tblBookings);
    }
}
